package com.sgb.member.view_model;

import android.text.TextUtils;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.sgb.member.MemberConfig;
import com.sgb.member.entity.MemberDoorDetailBean;
import com.sgb.member.entity.ParamBean;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AddMemberDoorModel extends ViewModel {
    public MutableLiveData<String> msgLiveData = new MutableLiveData<>();
    public MutableLiveData<Boolean> isLoadLiveData = new MutableLiveData<>();
    public MutableLiveData<List<CommonPopEntity>> memberTypes = new MutableLiveData<>();
    public MutableLiveData<Boolean> isSuccessLiveData = new MutableLiveData<>();
    public MutableLiveData<List<MemberDoorDetailBean>> detailLiveData = new MutableLiveData<>();

    public void submitData(String relation, String fisrtCategory, String secondCategory, String thirdCategory, List<ParamBean> viewList) {
        isLoadLiveData.postValue(true);
        Map<String, Object> map = new HashMap<>();
        Map<String, String> map1;
        map.put("relation", relation);//供需关系1:供方2:需方
        map.put("firstCategory", fisrtCategory);
        map.put("secondCategory", secondCategory);
        map.put("thirdCategory", thirdCategory);
        List list = new ArrayList();
        if (viewList.size() > 0) {
            for (int i = 0; i < viewList.size(); i++) {
                map1 = new HashMap<>();
                map1.put("isSupportDoc", viewList.get(i).isNeedFile());
                map1.put("requiredContent", viewList.get(i).getContent());
                list.add(map1);
            }
        }
        map.put("memberThresholdRequireList", list);
        MemberConfig.getInstance().getApi().addOrUpdate(map).enqueue(new ICallBack<BaseEntity<BaseEntity>>() {
            @Override
            public void onSuccess(BaseEntity<BaseEntity> data) {
                isLoadLiveData.postValue(false);
                if (data != null && data.getData() != null) {
                    isSuccessLiveData.postValue(true);
                } else {
                    msgLiveData.postValue(data.getMsg());
                }
            }

            @Override
            public void onFail() {
                super.onFail();
                isLoadLiveData.postValue(false);
            }
        });
    }

    // 会员类型
    public void getMemberTypeList(String ascription) {
        // 0 供方 1需方
        isLoadLiveData.postValue(true);
        String type = TextUtils.equals("1", ascription) ? "0" : "1";
        MemberConfig.getInstance().getApi().getMemberTypeList(type).enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                isLoadLiveData.postValue(false);
                if (data.getData() != null && data.getData() != null && data.getData().size() > 0) {
                    data.getData().add(0, new CommonPopEntity("", "全部"));
                    memberTypes.setValue(data.getData());
                }
            }

            @Override
            public void onFail() {
                isLoadLiveData.postValue(false);
                msgLiveData.postValue("请求失败!");
            }
        });
    }


    // 获取会员门槛列表根据会员类型
    public void getThreshold(String relation, String firstCategoryNo, String secondCateNo, String thirdCategoryNo) {
        Map<String, Object> map = new HashMap<>();
        map.put("relation", relation);
        if (!TextUtils.isEmpty(firstCategoryNo)) {
            if (!TextUtils.isEmpty(secondCateNo)) {
                if (!TextUtils.isEmpty(thirdCategoryNo)) {
                    firstCategoryNo = "";
                    secondCateNo = "";
                } else {
                    firstCategoryNo = "";
                }
            }
        }
        if (!TextUtils.isEmpty(firstCategoryNo)) {
            map.put("firstCategory", firstCategoryNo);
        }
        if (!TextUtils.isEmpty(secondCateNo)) {
            map.put("secondCategory", secondCateNo);
        }
        if (!TextUtils.isEmpty(thirdCategoryNo)) {
            map.put("thirdCategory", thirdCategoryNo);
        }
        MemberConfig.getInstance().getApi().getThreshold(map).enqueue(new ICallBack<BaseEntity<List<MemberDoorDetailBean>>>() {
            @Override
            public void onSuccess(BaseEntity<List<MemberDoorDetailBean>> data) {
                if (data != null && data.getData() != null) {
                    detailLiveData.postValue(data.getData());
                }else {
                    detailLiveData.postValue(new ArrayList<>());
                }
            }
        });
    }

}
